#!/bin/bash
#用户名
USERNAME='user'

#密码
PASSWORD='pass'

#其他参数(如--dev指定网卡，可运行zruijie -h查看详细参数)
ARGS=(--dhcp -b)

#---------------------------------------------------------------------
LOGFILE=/tmp/zruijie.log
MSGLOG="$HOME/.ruijielog"

echo "Beginning Authentication... "

echo $PASSWORD|zruijie $@ ${ARGS[@]} -u $USERNAME 2>&1|tee -i "$LOGFILE"

program_err=`sed -n -e '/^@@ERROR/p' $LOGFILE`
server_info=`sed -n -e '/^>>Server/p' $LOGFILE`
account_info=`sed -n -e '/^>>Account/{N;s/\n//;p}' $LOGFILE`

notify(){
    notify-send "锐捷" "$1";
    if [ $? -eq 127 ]; then
        echo "\`notify-send' not installed."
        echo "$1"
    fi
}

for msg in "$program_err" "$server_info" "$account_info"; do
    if [[ $msg ]]; then
        notify "${msg##>>}"
    fi
done

if [[ -e $MSGLOG ]];then
    lastlog=`tail -n 1 $MSGLOG|sed -e 's/^[-: 0-9]\+#//'`
fi

if [[ $server_info && $server_info != $lastlog ]]; then
    echo "$(date '+%F %T')#$server_info">>$MSGLOG;
fi


